#INCLUDE "PROTHEUS.CH"

USER FUNCTION CriaCD1()

PROCESSA({|| ProcRot()}, "Aguarde", "Incluindo Dados", .F.)

RETURN NIL

//-----------------------------------------------------------------
STATIC FUNCTION ProcRot()

dbSelectArea("CD1")
CD1->(dbSetOrder(1))  // CD1_FILIAL+CD1_CODNAT

dbSelectArea("SX5")  // X5_FILIAL+X5_TABELA+X5_CHAVE
SX5->(dbSetOrder(1))

IF !SX5->(dbSeek(xFilial("SX5") + "13"))
	RETURN NIL
ENDIF

WHILE !SX5->(EOF()) .And. SX5->X5_TABELA == "13"
	IF LEN(RTRIM(SX5->X5_CHAVE)) <> 4
		SX5->(dbSkip())
		LOOP
	ENDIF
	
	IF CD1->(dbSeek(XFILIAL("CD1") + SX5->X5_CHAVE))  // CD1_FILIAL+CD1_CODNAT
		SX5->(dbSkip())
		LOOP
	ENDIF
	
	CD1->(RecLock("CD1", .T.))
		CD1->CD1_CODNAT := SX5->X5_CHAVE
		CD1->CD1_DESCR := SX5->X5_DESCRI
	CD1->(MsUnlock())
	
	SX5->(dbSkip())
END WHILE

RETURN NIL

//-----------------------------------------------------------------